<template>
  <div class="login">
    <div class="login_box">
      <div class="logo_box">
        <img src="../assets/logo.png" alt="" />
      </div>

      <div class="form_box" ref="mydiv">
        <!-- model: 表单数据对象 -->
        <!-- rules: 表单验证规则对象 -->
        <el-form
          :rules="form.rules"
          ref="form"
          class="demo-ruleForm"
          :model="form.data"
        >
          <!-- prop: 表单数据的键名 -->
          <el-form-item prop="username">
            <el-input
              type="text"
              autocomplete="off"
              v-model="form.data.username"
              placeholder="请输入用户名"
              prefix-icon="el-icon-user-solid"
            ></el-input>
            <!-- autocomplete 原生属性，自动补全 -->
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              autocomplete="off"
              v-model="form.data.password"
              prefix-icon="el-icon-lock"
              placeholder="请输入密码"
              @keyup.enter.native="submit"
            ></el-input>
          </el-form-item>
          <el-form-item class="btn_box">
            <el-button type="primary" @click="submit">提交</el-button>
            <el-button @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      form: {
        data: {
          username: '',
          password: '',
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            {
              min: 3,
              max: 5,
              message: '长度在 3 到 5 个字符',
              trigger: 'blur',
            },
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            {
              min: 6,
              max: 12,
              message: '长度在 6 到 12 个字符',
              trigger: 'blur',
            },
          ],
        },
      },
    };
  },
  methods: {
    submit() {
      this.$refs.form
        .validate()
        .then(() => {
          return this.__apis.login(this.form.data);
        })
        .then((res) => {
          if (res.data.meta.status === 200) {
            this.$message({
              message: res.data.meta.msg,
              type: 'success',
            });
            localStorage.setItem('token', res.data.data.token);
            this.$router.push('/');
          } else {
            this.$message.error(res.data.meta.msg);
          }
        })
        .catch(() => {
          // console.log(res);
        });
    },
    reset() {
      this.$refs.form.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  background-color: #2b4b6b;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  .login_box {
    width: 450px;
    height: 304px;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: #fff 0 0 5px 1px;
    position: relative;
    .logo_box {
      position: absolute;
      transform: translate(-50%, -50%);
      left: 50%;
      width: 150px;
      height: 150px;
      box-sizing: border-box;
      background-color: #fff;
      border-radius: 50%;
      box-shadow: #ddd 0 0 10px 1px;
      overflow: hidden;
      padding: 10px;
      img {
        width: 100%;
        border-radius: 50%;
        background-color: #efefef;
      }
    }
    .form_box {
      padding: 20px;
      width: 100%;
      position: absolute;
      box-sizing: border-box;
      bottom: 0;
      .btn_box {
        display: flex;
        justify-content: flex-end;
        margin-bottom: 0;
      }
    }
  }
}
</style>
